/*
 * @(#)XfslAlgorithmDialog.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.view;

import xfuzzy.util.*;
import xfuzzy.xfsl.model.XfslAlgorithm;
import xfuzzy.xfsl.model.XfslAlgorithmParam;
import xfuzzy.xfsl.view.listener.XfslAlgorithmActionListener;

import javax.swing.*;
import java.awt.*;

/**
 * Ventana de configuraci�n de los algoritmos de aprendizaje
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfslAlgorithmDialog extends JDialog {

	//----------------------------------------------------------------------------//
	//                            COSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603064L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Algoritmo a configurar
	 */
	private XfslAlgorithm algorithm;
	
	/**
	 * Par�metros del algoritmo
	 */
	private XfslAlgorithmParam param[];
	
	/**
	 * Campos de introducci�n de los valores de los par�metros
	 */
	private XTextForm[] text;
		
	/**
	 * Flag para indicar si ha sido seleccionado
	 */
	private boolean selected;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfslAlgorithmDialog(JFrame frame, XfslAlgorithm algorithm) {
		super((Frame) frame, "Xfsl", true);
		this.algorithm = algorithm;
		this.param = algorithm.getParams();
		build(frame);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Verifica si los valores se han selecionado
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * Selecciona los valores de la ventana
	 */
	public void set() {
		if(!get()) XDialog.showMessage(null,"Values introduced are not correct");
		else { selected = true; setVisible(false); }
	}

	/**
	 * Cierra la ventana sin seleccionar los valores
	 */
	public void cancel() {
		selected = false;
		setVisible(false);
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lee los valores de la ventana
	 */
	private boolean get() {
		boolean good = true;
		double[] newparam = new double[param.length];
		for(int i=0; i<param.length; i++) {
			boolean pgood = true;
			int test = param[i].getKind();
			try {
				if(test == XfslAlgorithm.INTEGER) {
					newparam[i] = Integer.parseInt(text[i].getText());
					if(newparam[i]<1) pgood = false;
				}
				else newparam[i] = XfslAlgorithm.test(Double.parseDouble(text[i].getText()), test);
			}
			catch(Exception ex) { pgood = false; }
			if(!pgood) {
				text[i].setText("");
				good = false;
			}
		}
		if(good) {
			try { algorithm.setParameters(newparam); } catch(Exception ex) { good = false; }
		}
		return good;
	}

	/**
	 * Construye la ventana
	 */
	private void build(JFrame frame) {
		this.text = new XTextForm[param.length];
		for(int i=0; i<param.length; i++) {
			text[i] = new XTextForm(param[i].getName());
			double value = param[i].getValue();
			if(value < 0) text[i].setText("");
			else if( param[i].getKind() == XfslAlgorithm.INTEGER) text[i].setText(""+ (int) value);
			else text[i].setText(""+value);
		}
		XTextForm.setWidth(text);

		String label[] = { "Set", "Cancel" };
		XCommandForm form = new XCommandForm(label,label,new XfslAlgorithmActionListener(this));
		form.setCommandWidth(100);

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel(algorithm.getName()+" Algorithm"));
		for(int i=0; i<text.length; i++) content.add(text[i]);
		content.add(form);

		Point loc = frame.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		setLocation(loc);
		pack();
	}
}
